<?php
	function array_insert(&$array, $insert, $position = -1) {
		 $position = ($position == -1) ? (count($array)) : $position ;
		 if($position != (count($array))) {
			  $ta = $array;
			  for($i = $position; $i < (count($array)); $i++) {
				   if(!isset($array[$i])) {
						die(print_r($array, 1)."\r\nInvalid array: All keys must be numerical and in sequence.");
				   }
				   $tmp[$i+1] = $array[$i];
				   unset($ta[$i]);
			  }
			  $ta[$position] = $insert;
			  $array = $ta + $tmp;
			  //print_r($array);
		 } else {
			  $array[$position] = $insert;         
		 }
			 
		 ksort($array);
		 return true;
	}
 function search_in_array ($needle, $haystack, $inverse = false, $limit = 50) {
   
    # Settings
    $path = array ();
    $count = 0;
   
    # Check if inverse
    if ($inverse == true)
        $haystack = array_reverse ($haystack, true);
       
    # Loop
    foreach ($haystack as $key => $value) {

        # Check for return
        if ($count > 0 && $count == $limit)   
            return $path;
       
        # Check for val
        if ($value === $needle) {
           
            # Add to path
            $path[] = $key;
           
            # Count
            $count++;
           
        } else if (is_array ($value)) {
           
            # Fetch subs
            $sub = search_in_array ($needle, $value, $inverse, $limit);
           
            # Check if there are subs
            if (count ($sub) > 0) {
               
                # Add to path
                $path[$key] = $sub;
               
                # Add to count
                $count += count ($sub);
            }
        }
    }
   
    return $path;
}
  function insorter_nocase(&$array, $item = '', $defineStartOffset = 0, $andInsertToo = 0) {
            /* Presumes: 1) $array is ORDERED. 2) $array is ordered from SMALLER to BIGGER. */
            //Initialize:
            $originalItem = $item;
            $item = strtolower($item);
            $lastIndex = $currentTopLimit = sizeof($array);
            --$lastIndex;
            $currentMiddle = floor($currentTopLimit / 2);
            if ($defineStartOffset) {
                    $currentMiddle = floor((($lastIndex - $defineStartOffset) / 2) + $defineStartOffset);
            }
            //RUN:
            while ($currentMiddle > 0 && $currentMiddle < $lastIndex) {
                    $noCase = strtolower($array[$currentMiddle]);
                    if ($noCase > $item) {
                            /*search backward*/
                            $currentTopLimit = $currentMiddle;
                            $currentMiddle = floor($currentMiddle / 2);
                            $noCase = strtolower($array[$currentMiddle]);
                    } elseif ($noCase <= $item) {
                            /*search forward*/
                            $currentMiddle = floor((($currentTopLimit - $currentMiddle) / 2) + $currentMiddle);
                            $noCase = strtolower($array[$currentMiddle]);
                    }
                    if ($noCase <= $item && strtolower($array[$currentMiddle + 1]) > $item) {
                            /*offset found*/break;
                    }
            }
            //Streamline:
            if (!$currentMiddle && isset($array[$currentMiddle]) && strtolower($array[$currentMiddle]) > $item) {
                    --$currentMiddle;
            }
            if ($andInsertToo) {
                    if (sizeof($array) == 2 && $item >= strtolower($array[0]) && $item < strtolower($array[1])) {
                            array_splice($array, 1, 0, array($originalItem));
                    } elseif (sizeof($array) == 2 && $item <= strtolower($array[0])) {
                            array_unshift($array, $originalItem);
                    } elseif ($currentMiddle < 0) {
                            array_unshift($array, $originalItem);
                    } elseif ($currentMiddle >= $lastIndex) {
                            array_push($array, $originalItem);
                    } else {
                            array_splice($array, $currentMiddle + 1, 0, array($originalItem));
                    }
            }
            return $currentMiddle;
            /* keep this comment to reuse freely:
             http://www.fullposter.com/?1 */
    }
?>